package com.tanhua.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.service.RecommendUserService;
import com.tanhua.server.service.TodayBestService;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TodayBestServiceImpl implements TodayBestService {
    @Autowired
    private RecommendUserService recommendUserService;
    @Value("${tanhua.sso.default.user}")
    private Long defaultUser;
    @Autowired
    private UserInfoService userInfoService;
    @Override
    public TodayBest queryTodayBest() {
        User user= UserThreadLocal.get();

        TodayBest todayBest=this.recommendUserService.queryTodayBest(user.getId());
        if(null==todayBest){
            todayBest=new TodayBest();
            todayBest.setId(defaultUser);
            todayBest.setFateValue(80L);
        }
        //补全个人信息
        UserInfo userInfo=userInfoService.queryUserInfoById(todayBest.getId());
        if(null==userInfo){
            return null;
        }
        todayBest.setAge(userInfo.getAge());
//        todayBest.setArgs(userInfo.getTags());
        todayBest.setAvatar(userInfo.getLogo());
        todayBest.setNickname(userInfo.getNickName());
        todayBest.setTags(StringUtils.split(userInfo.getTags(), ','));
        todayBest.setSex(userInfo.getSex().getValue()==1?"man":"woman");
       // TodayBest todayBest=new TodayBest();
        return todayBest;
    }

    /**
     * 查询推荐列表信息
     * @param queryParam
     * @return
     */
    @Override
    public PageResult queryRecommendation(RecommendUserQueryParam queryParam) {
        User user =UserThreadLocal.get();
        //开始封装pageResult对象
        PageResult pageResult=new PageResult();
        pageResult.setPage(queryParam.getPage());
        pageResult.setPagesize(queryParam.getPageSize());
        PageInfo<RecommendUser> pageInfo=this.recommendUserService.queryRecommendation(user.getId(),queryParam.getPage(),queryParam.getPageSize());
        List<RecommendUser> records = pageInfo.getRecords();
        if(null==records){
            return  null;
        }
        Set<Long> set=new HashSet<>();
        for (RecommendUser record : records) {
            set.add(record.getUserId());
        }
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id",set);
        if(StringUtils.isEmpty(queryParam.getGender())){
            queryWrapper.eq("sex",StringUtils.equals(queryParam.getGender(),"man")?1:2);
        }
        if(StringUtils.isEmpty(queryParam.getCity())){
            queryWrapper.like("city",queryParam.getGender());
        }

        if(null!=queryParam.getAge()){
            queryWrapper.le("age",queryParam.getAge());
        }
        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoList(queryWrapper);
        if (null==userInfoList){
            return pageResult;
        }
        List<TodayBest> todayBests = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            TodayBest todayBest=new TodayBest();
            todayBest.setAge(userInfo.getAge());
            todayBest.setId(userInfo.getUserId());
            todayBest.setAvatar(userInfo.getLogo());
            todayBest.setNickname(userInfo.getNickName());
            todayBest.setTags(StringUtils.split(userInfo.getTags(), ','));
            todayBest.setSex(userInfo.getSex().getValue()==1?"man":"woman");
            //缘分值
            for (RecommendUser record : records) {
                if(record.getUserId().longValue() == userInfo.getUserId().longValue()){
                    double score = Math.floor(record.getScore());//取整,98.2 -> 98
                    todayBest.setFateValue(Double.valueOf(score).longValue());
                    break;
                }
            }
            todayBests.add(todayBest);
        }

        Collections.sort(todayBests,(o1, o2) -> new Long(o2.getFateValue()-o1.getFateValue()).intValue());
        pageResult.setItems(todayBests);
        return pageResult;
    }
}
